<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 *
 * Created on	:  Aug 15, 2008
 *
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RLibImporter::file ( "phpmailer/class.phpmailer.php" );

class RMailHelper {

	/**
	 * Send email using PHPMailer
	 * if the input params in null, normally it will get data from configuration instead
	 *
	 * @param string $toEmail
	 * @param string $subject
	 * @param string $body
	 * @param string $toName
	 * @param string $AltBody
	 * @param string $fromEmail
	 * @param string $fromName
	 * @param string $mailer
	 * @param string $smtphost
	 * @param string $smtpuser
	 * @param string $smtppass
	 * @param string $port
	 * @param string $sendmail
	 * @return boolean
	 */
	static function sentMail($toEmail, $subject, $body, $toName = '', $AltBody = '', $fromEmail = null, $fromName = null, $mailer = null, $smtphost = null, $smtpuser = null, $smtppass = null, $port = null, $sendmail = null, $charset = 'utf-8') {

		$mail = RMailHelper::getPhpMailerObject ( $fromEmail, $fromName, $mailer, $smtphost, $smtpuser, $smtppass, $port, $sendmail );

		$mail->AddAddress ( $toEmail, $toName );
		$mail->CharSet = $charset;
		$mail->Subject = $subject;
		$mail->Body = $body;
		$mail->AltBody = $AltBody;

		return $mail->Send ();
	}

	/**
	 * Create the phpMailer Object
	 * if the input params in null, normally it will get data from configuration instead
	 *
	 * @param string $toEmail
	 * @param string $subject
	 * @param string $body
	 * @param string $toName
	 * @param string $AltBody
	 * @param string $fromEmail
	 * @param string $fromName
	 * @param string $mailer
	 * @param string $smtphost
	 * @param string $smtpuser
	 * @param string $smtppass
	 * @param string $port
	 * @param string $sendmail
	 * @return PHPMailer
	 */
	static function createPhpMailerObject($toEmail, $subject, $body, $toName = '', $AltBody = '', $fromEmail = null, $fromName = null, $mailer = null, $smtphost = null, $smtpuser = null, $smtppass = null, $port = null, $sendmail = null) {

		$mail = RMailHelper::getPhpMailerObject ( $fromEmail, $fromName, $mailer, $smtphost, $smtpuser, $smtppass, $port, $sendmail );

		$mail->AddAddress ( $toEmail, $toName );

		$mail->Subject = $subject;
		$mail->Body = $body;
		$mail->AltBody = $AltBody;

		return $mail;
	}

	/**
	 * Get the PHPMailer object with the input information.
	 * If some infor is null --> the configuration will be load instead
	 *
	 * @param string $fromEmail
	 * @param string $fromName
	 * @param string $mailer
	 * @param string $smtphost
	 * @param string $smtpuser
	 * @param string $smtppass
	 * @param string $port
	 * @param string $sendmail
	 * @return PHPMailer
	 */
	static function getPhpMailerObject($fromEmail = null, $fromName = null, $mailer = null, $smtphost = null, $smtpuser = null, $smtppass = null, $port = null, $sendmail = null) {
		$conf = SystemSite::getSiteConfig ();
		if ($fromEmail === null) {
			$fromEmail = $conf->mailfrom;
		}
		
		if ($fromName === null) {
			$fromName = $conf->fromname;
		}
		if ($mailer === null) {
			$mailer = $conf->mailer;
		}
		if ($sendmail === null) {
			$sendmail = $conf->sendmail;
		}
		if ($smtpuser === null) {
			$smtpauth = $conf->smtpauth;
		}
		if ($smtphost === null) {
			$smtphost = $conf->smtphost;
		}
		if ($port === null) {
			$port = $conf->mailport;
		}
		if ($smtpuser === null) {
			$smtpuser = $conf->smtpuser;
		}
		if ($smtppass === null) {
			$smtppass = $conf->smtppass;
		}

		$mail = new PHPMailer ( );

		$mail->From = $fromEmail;
		$mail->FromName = $fromName;

		$mail->Sendmail = $sendmail;
		$mail->Mailer = $mailer;

		$mail->Host = $smtphost;
		$mail->Port = $port;

		$mail->SMTPAuth = $smtpauth;
		$mail->Username = $smtpuser;
		$mail->Password = $smtppass;
		return $mail;
	}
}
?>
